package se.energimyndigheten.lampguiden.bulbreplacement;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.actionbarsherlock.app.SherlockActivity;

import se.energimyndigheten.lampguiden.R;
import se.energimyndigheten.lampguiden.DashboardActivity;

// Suppressing a few things that currently are not needed
@SuppressLint("SetJavaScriptEnabled")
public class ResultWebActivity extends SherlockActivity {

    public static final String EXTRA_LIGHT_SOURCE = "light_source";
    public static final String EXTRA_KELVIN = "kelvin_index";
    public static final String EXTRA_LUMEN = "lumen_index";

    private static final String TAG = ResultWebActivity.class.getSimpleName();
    private static final String HTML_PATH = "../sv_resultset.html";
    private static final int INVALID_EXTRA = Integer.MIN_VALUE;

    private static final String[] LIGHT_SOURCE = { "led", "halogen", "le" };
    @SuppressWarnings("unused")
    private static final String[] KELVIN_VALUES = { "kelvin_1", "kelvin_2", "kelvin_3" };
    @SuppressWarnings("unused")
    private static final String[] LUMEN_VALUES = { "lumen_1", "lumen_2", "lumen_3" };

    private WebView mWebView;
    private int lightSource;
    @SuppressWarnings("unused")
    private int kelvinIndex;
    @SuppressWarnings("unused")
    private int lumenIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // handle incoming intent
        try {
            final Intent intent = getIntent();
            lightSource = intent.getIntExtra(EXTRA_LIGHT_SOURCE, INVALID_EXTRA);
            lumenIndex = intent.getIntExtra(EXTRA_LUMEN, INVALID_EXTRA);
            kelvinIndex = intent.getIntExtra(EXTRA_KELVIN, INVALID_EXTRA);
        } catch (NullPointerException npe) {
            Log.e(TAG, "NullPointerException - missing intent or invalid extras");
        }

        // TODO - validate values from intent - may not be INVALID_EXTRA

        setContentView(R.layout.webview_activity);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        
        // make some divs visible when page is loaded
        final String path = DashboardActivity.HTML_BASEPATH + HTML_PATH;
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {

                // FIXME: temp fix to handle /../ in path
                if (url.endsWith(path.replace("submodule_html/../", ""))) {
                    final String $ID = "document.getElementById";
                    final String DOM_DISPLAY = ".style.display='';";
                    StringBuilder sb = new StringBuilder("javascript:(function(){");

                    // make elements visible
                    sb.append($ID).append("('").append(LIGHT_SOURCE[lightSource]).append("')").append(DOM_DISPLAY);
                    // sb.append($ID).append("('").append(KELVIN_VALUES[kelvinIndex]).append("')").append(DOM_DISPLAY);
                    // sb.append($ID).append("('").append(LUMEN_VALUES[lumenIndex]).append("')").append(DOM_DISPLAY);

                    sb.append("})();");
                    view.loadUrl("javascript:document.getElementById('led').style.display='';");
                    // view.loadUrl(js);

                } else {
                    Log.v(TAG, " - url did not match");
                }
            }

         });

        mWebView.loadUrl(path);
    }
}
